package com.zjlanyun.erp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zjlanyun.erp.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author: shun
 * @date: 2016-09-28 08:00
 * @Desc: 通用列表Adapter
 */
public class CommonAdapter extends BaseAdapter {

    private ArrayList<HashMap<String, Object>> mList;
    private LayoutInflater mInflater;
    private int[] layout;
    private Context mContext;
    private String[] field;

    public CommonAdapter(Context context, ArrayList<HashMap<String, Object>> list, int[] layout, String[] field) {
        this.mContext = context;
        this.mList = list;
        this.mInflater = LayoutInflater.from(mContext);
        this.layout = layout;
        this.field = field;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ArrayList<TextView> tvList = new ArrayList<>();
        convertView = mInflater.inflate(R.layout.common_list_item, null);
        for (int i = 0; i < layout.length; i++) {
            tvList.add((TextView) convertView.findViewById(layout[i]));
            tvList.get(i).setVisibility(View.VISIBLE);
        }
        HashMap<String, Object> map = mList.get(position);
        for (int i = 0; i < field.length; i++) {
            TextView tv = tvList.get(i);
            tv.setText(String.valueOf(map.get(field[i])));
        }
        return convertView;
    }
}
